
//
//  DDStatusCell.swift
//  SinaMicroBlogSwift2
//
//  Created by dqw on 15/8/3.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

let DDStatusCellMargin:CGFloat = 8.0
enum DDStatusCellId: String {
    
    case normalId = "normalCell"
    case forwardId = "forwardCell"
    
    static func cellId(status: DDStatus) -> String? {
        return (status.retweeted_status == nil) ? DDStatusCellId.normalId.rawValue : DDStatusCellId.forwardId.rawValue
    }
}

class DDStatusCell: UITableViewCell {

    var status: DDStatus? {
        didSet {
            let text = status?.text ?? ""
            contentLabel.attributedText = DDEmoticonPackage.attributedText(text, font: contentLabel.font)
            
            topView.status = status
            pictureView.status = status
            
            pictureViewHeightCont?.constant = pictureView.bounds.height
            pictureViewWidthCont?.constant = pictureView.bounds.width
            pictureViewTopCont?.constant = (pictureView.bounds.height == 0) ? 0 : DDStatusCellMargin
        }
    }
    
     var pictureViewHeightCont: NSLayoutConstraint?
     var pictureViewWidthCont: NSLayoutConstraint?
     var pictureViewTopCont: NSLayoutConstraint?
    
    
    func rowHeight(status: DDStatus) -> CGFloat {
        
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottonView.frame)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addAndLayoutSubViews()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    
     func addAndLayoutSubViews() {
        contentView.addSubview(contentLabel)
        contentView.addSubview(topView)
        contentView.addSubview(bottonView)
        contentView.addSubview(pictureView)
        
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: DDStatusCellMargin))
        
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * DDStatusCellMargin
        
//       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: DDStatusCellMargin))
//        
//        pictureViewHeightCont = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureViewWidthCont = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width )
//        pictureViewTopCont = pictureView.ff_Constraint(cons
//            , attribute: NSLayoutAttribute.Top )
        

        bottonView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -DDStatusCellMargin, y: DDStatusCellMargin))
        
        //FIXME: 如果要计算Cell的高度，则Cell和司空见的尺寸就不能约束。
//        bottonView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPointZero)

        

    }
    
     lazy var contentLabel: UILabel = UILabel(color: UIColor.darkTextColor(), fontSize: 14)
     lazy var topView: DDStatusCellTopView = DDStatusCellTopView()
     lazy var bottonView: DDStatusCellBottonView = DDStatusCellBottonView()
     lazy var pictureView: DDStatusCellPictureView = DDStatusCellPictureView()
    
    
    
}
